﻿var com = com || {};
com.openTab = parent.addTab,
com.ajax = function (obj) {
    var me = this;
    me.url = obj.url || "";
    me.data = obj.data || "";
    me.type = obj.type || "POST";
    me.success = obj.success || function (data, status, xhr) {
    };
    $.ajax({
        type: me.type,
        url: me.url,
        data: me.data,
        success: me.success
    });
},
com.close = function () {
    var parent = top || window;
    var ifrs = parent.document.getElementsByTagName("iframe");
    for (var i = 0; i < ifrs.length; ++i) {
        if (ifrs[i].contentWindow == window) {
            parent.closeTab(ifrs[i].title);
            break;
        }
    }
},
com.topParentMsg = function (iconCls, msgText, fn) {
    if (iconCls == "confirm") {
        com.showComfirm(iconCls, msgText, fn);
    } else {
        com.showInPageTop(iconCls, msgText);
    }
},
com.message = function (msgType, msgText, fn) {
    var me = this;
    me.msgText = msgText || "";
    me.msgType = msgType;
    me.iconCls = "icon-information";
    switch (msgType) {
        case "information":
            me.iconCls = "icon-information";
            break;
        case "warning":
            me.iconCls = "icon-warn";
            break;
        case "error":
            me.iconCls = "icon-error";
            break;
        case "success":
            me.iconCls = "icon-success";
            break;
        case "confirm":
            me.iconCls = "confirm";
            break;
        default:
            break;
    }

    parent.message(me.iconCls, msgText, fn);
},
com.showInPageTop = function (iconCls, msgText) {
    var me = this;
    me.iconCls = iconCls || "icon-information";
    me.msgText = msgText;
    if (!me.msgText) return;

    var msgDiv = $("<div class='show-msg' style='width:200px;'><table style='width:100%'><tr><td><div class='" + me.iconCls + "' style='height:20px;width:30px;' ></div></td><td><div style='padding:0px 5px 0px 5px;'>" + msgText + "</div></td><td><div class='icon-close'  onclick='$(this).parent().parent().parent().parent().parent().remove();' style='height:20px;width:30px;' ></div></td></tr></table></div>");
    /*var parent;
    if (!top || !top.document) {
    parent = top.document.body;
    } else {
    parent = document.body;
    }
    */

    var clientW = parseFloat(document.body.clientWidth / 2);
    var thisW = parseFloat($(msgDiv).width() / 2);
    $(msgDiv).css('left', clientW - thisW);
    $(document.body).append(msgDiv);
    setTimeout(function () { $(msgDiv).remove(); }, 3000);
},
com.showComfirm = function (title, msgText, fn) {
    $.messager.confirm(title, msgText, function (b) {
        if (typeof (fn) == "function") {
            fn.apply(this, [b]);
        }
    });
},
com.editGridViewModel = function (grid) {
    var me = this;
    me.editIndex = undefined;
    me.addRow = function (rowData) {
        if (me.ended()) {
            if (Object.prototype.toString.call(rowData) != "[object Object]") rowData = {};
            rowData = $.extend({ _isnew: true }, rowData);
            grid.datagrid("appendRow", rowData);
            me.editIndex = grid.datagrid("getRows").length - 1;
            me.begin(me.editIndex);
        }
    };

    me.deleteRow = function () {
        var selectedRow = grid.datagrid("getSelected");
        if (selectedRow) {
            var selectedIndex = grid.datagrid("getRowIndex", selectedRow);
            if (selectedIndex == me.editIndex) {
                me.editIndex = undefined;
            }
            grid.datagrid("deleteRow", selectedIndex);
        }
    };

    me.begin = function (index, row) {
        if (index == undefined || typeof index === 'object') {
            row = grid.datagrid("getSelected");
            index = grid.datagrid("getRowIndex", row);
        }
        me.editIndex = me.ended() ? index : me.editIndex;
        grid.datagrid("selectRow", me.editIndex).datagrid("beginEdit", me.editIndex);
        if (grid.readonlyFieldsInEdit && grid.readonlyFieldsInEdit.length > 0) {
            $.each(grid.readonlyFieldsInEdit, function (idx, value) {
                var target = grid.datagrid("getEditor", { index: me.editIndex, field: value }).target;
                if (row && !row._isnew)
                    $(target).attr("readonly", "readonly");
            });
        }
    };

    me.ended = function () {
        if (me.editIndex == undefined) return true;
        if (grid.datagrid("validateRow", me.editIndex)) {
            grid.datagrid("endEdit", me.editIndex);
            me.editIndex = undefined;
            return true;
        }
        grid.datagrid("selectRow", me.editIndex);
        return false;
    };
    me.getChanges = function () {
        var inserted = grid.datagrid('getChanges', 'inserted'),
            updated = grid.datagrid('getChanges', 'updated'),
            deleted = grid.datagrid('getChanges', 'deleted');
        var changes = { inserted: inserted, updated: updated, deleted: deleted };
        changes._changed = (inserted.length + updated.length + deleted.length) > 0;
        return changes;
    };
};

$.extend($.fn.datagrid.defaults.editors, {
    button: {
        init: function (container, options) {
            var input = $('<input type="button" class="datagrid-editable-input">').appendTo(container);
            return input;
        },
        destroy: function (target) {
            $(target).remove();
        },
        getValue: function (target) {
            return $(target).val();
        },
        setValue: function (target, value) {
            $(target).val(value);
        },
        resize: function (target, width) {
            $(target)._outerWidth(width);
        }
    }
});